package com.example.tyz1201.myaccount.Activity.Activity.Activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;

import com.example.tyz1201.myaccount.Activity.Activity.Adapter.DifferenceAdapter;
import com.example.tyz1201.myaccount.Activity.Activity.Model.AssumeModel;
import com.example.tyz1201.myaccount.Activity.Activity.Model.DifferenceModel;
import com.example.tyz1201.myaccount.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by tyz1201 on 2016/4/12.
 */
public class MonthDifferenceActivity extends AppCompatActivity {

    private int choosedAssumeTypeId;
    private String assumeData, createTimeStr, showTimeStr;
    private long createTime;
    private AssumeModel assumeModel;
    private DifferenceModel differenceModel;
    private Date createDate;
    private Calendar createCalendar;
    List<DifferenceModel> monthDifferenceMessages = new ArrayList<>();
    Map<String, String> map = new HashMap<>();
    Toolbar toolbar_month_diff;
    ListView lv_month_diff;
    DifferenceAdapter differenceAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_diff);

        toolbar_month_diff = (Toolbar) findViewById(R.id.toolbar_month_diff);
        toolbar_month_diff.setTitle("本月收支情况");
        toolbar_month_diff.setTitleTextColor(getResources().getColor(R.color.Black));
        setSupportActionBar(toolbar_month_diff);

        lv_month_diff = (ListView) findViewById(R.id.lv_month_diff);

        initmonthDifferenceMessages();
        differenceAdapter = new DifferenceAdapter(monthDifferenceMessages, MonthDifferenceActivity.this);

        lv_month_diff.setAdapter(differenceAdapter);
    }

    private void initmonthDifferenceMessages() {
        SharedPreferences sharedPreferences = getSharedPreferences("account", MODE_PRIVATE);
        map = (Map<String, String>) sharedPreferences.getAll();
        Set<String> keySet = map.keySet();
        float dayMoney = 0;
        int dayOfMonth, createTimeInt;
        String createTimeStr1, createTimeStr2;


        for (String key : keySet) {
            assumeData = map.get(key);
            assumeModel = AssumeModel.backToAssumeModel(assumeData);
            choosedAssumeTypeId = assumeModel.getChoosedAssumeTypeId();  //实现 当日日期  当日差额
/**
 *     DifferenceModel(yy-MM-dd,float);  先判断日期再计算，月份，根据天数，实现赋值。判断是否一天，累加。
 */
            createTime = assumeModel.getCreateTime(); //long 类型日期

            createDate = new Date(createTime); // Date 类型日期

            SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd");
            createTimeStr = sf.format(createDate); //createTimeStr字符串类型日期

            SimpleDateFormat sf1 = new SimpleDateFormat("dd");
            createTimeStr1 = sf1.format(createDate);
            createTimeInt = Integer.parseInt(createTimeStr1);

            createCalendar = Calendar.getInstance();
            createCalendar.setTime(createDate); // Calendar 类型日期

            SimpleDateFormat sf2 = new SimpleDateFormat("yy-MM");
            createTimeStr2 = sf2.format(createDate);
            showTimeStr = createTimeStr2;

/**
 *        计算当月每天差额！
 */
            dayOfMonth = createCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            monthDifferenceMessages.clear();
            for (int i = 1; i <= dayOfMonth; i++) {
                if (createTimeInt == i) {
                    if (choosedAssumeTypeId == -1) {
                        dayMoney = dayMoney + choosedAssumeTypeId * assumeModel.getTodoMoney();
                    } else if (choosedAssumeTypeId == 1) {
                        dayMoney = dayMoney + choosedAssumeTypeId * assumeModel.getTodoMoney();
                    }

                    String str = dateHandle(i);
                    differenceModel = new DifferenceModel(showTimeStr + "-" + str, dayMoney);
                    monthDifferenceMessages.add(differenceModel);
                } else {
                    String str = dateHandle(i);
                    differenceModel = new DifferenceModel(showTimeStr + "-" + str, 0);
                    monthDifferenceMessages.add(differenceModel);
                }

            }
            differenceModel = new DifferenceModel("合  计 ：", dayMoney);
            monthDifferenceMessages.add(differenceModel);
        }
    }


    private String dateHandle(int i) {
        String str = i + "";
        if (i < 10) {
            str = "0" + i;
        }
        return str;
    }

}

